package com.junzhiit.nengji.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.bo.PostageTemplateBO;
import com.junzhiit.nengji.dao.PostageTemplateDAO;
import com.junzhiit.nengji.model.PostageTemplate;

@Repository
public class PostageTemplateDAOImpl extends AbstractBaseDAO<PostageTemplate> implements PostageTemplateDAO {

	@Override
	public List<PostageTemplate> findAll(int pageNumber, int maxPageRow,String shopId) {
		StringBuilder sql=new StringBuilder();
		
		sql.append(" select * from postage_template ");
		sql.append(" where is_delete=? and shop_id = ?");
		
		StatementParameter params = new StatementParameter(); 
		params.setInt(IsDelete.NO.toIntValue());
		params.setString(shopId);
		
		
		
		return queryForPagingList(sql.toString(), PostageTemplate.class, params,pageNumber, maxPageRow);
	}

	@Override
	public Integer getTotalCount(String shopId) {
		StringBuffer sql = new StringBuffer();
		sql.append("select count(id) from `postage_template`");
		sql.append(" where is_delete=? and shop_id=?");

		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());
		params.setString(shopId);

		return queryForInt(sql.toString(), params);
	}
	
	@Override
	public List<PostageTemplate> getAll(String shopId){
		StringBuffer sql=new StringBuffer();
		sql.append("select * from `postage_template` where is_delete=? and shop_id=?");
		
		StatementParameter params=new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());
		params.setString(shopId);
		return queryForList(sql.toString(),PostageTemplate.class,params);
	}
	
	@Override
	public List<PostageTemplateBO> getAllBO(PostageTemplate postageTemplate){
		StringBuffer sql=new StringBuffer();
		sql.append(" select pt.id,pt.name,pt.valuate_type,pc.area_json,pc.postage_template_id,pc.area_content,pc.basic_quality,pc.basic_postage,pc.increment_quality,pc.increment_postage,");
		sql.append(" (select count(*) from goods where postage_template_id=pt.id) use_count");
		sql.append(" from `postage_template` pt");
		sql.append(" left join `postage_condition` pc on pc.postage_template_id=pt.id");
		sql.append(" where pt.is_delete=? and pc.is_delete=? and shop_id=?");
		
		StatementParameter params=new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());
		params.setInt(IsDelete.NO.toIntValue());
		params.setString(postageTemplate.getShopId());
		
		if(StringUtils.isNotBlank(postageTemplate.getId())){
			sql.append(" and pt.id=? ");
			params.setString(postageTemplate.getId());
		}
		
		return queryForList(sql.toString(),PostageTemplateBO.class,params);
	}
	
	

}
